import { SubscribeMessage, WebSocketGateway, WebSocketServer } from "@nestjs/websockets";
import { Server } from "socket.io";

@WebSocketGateway(10001, { namespace: "dicomServer" })
export class AppGateway {
  @WebSocketServer()
  server: Server;

  client;

  handleConnection(client: any) {
    console.log("Client connected:", client.id);
    this.client = client;
  }

  handleDisconnect(client: any) {
    console.log("Client disconnected:", client.id);
    this.client = null;
  }

  sendDicomMsg(msg) {
    this.sendMsg("dicomServerInfo", msg);
  }

  sendSysMsg(msg) {
    this.sendMsg("systemInfo", msg);
  }

  private sendMsg(type, msg) {
    if (this.client) {
      this.client.emit(type, msg);
    }
  }


  @SubscribeMessage("message")
  handleMessage(client: any, payload: any): string {
    return "Hello world!";
  }
}
